package com.zhangtai.modules.dao;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhangtai.modules.dto.EmployeeInsuranceDto;
import com.zhangtai.modules.dto.InsuranceMemberRecordDto;
import com.zhangtai.modules.entity.insurance.EmployeeInsuranceEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-11-23 14:28:02
 */
@Mapper
public interface EmployeeInsuranceDao extends BaseMapper<EmployeeInsuranceEntity> {

    @Update("update tb_employee_insurance set status = 2 where insurance_id = #{insuranceId}")
    void removeAllEmployeeInsurance(@Param("insuranceId") Long insuranceId);

    @Select("<script>select t1.id,t1.oid,t1.insurance_id,t2.code,t2.name,t2.gender,t2.idcard,t2.job_type,t2.employeestatus,t2.job_level_id," +
            "t2.hiredate,t2.org_id,t2.unit_id,t2.job_id,t1.status,t3.company_name,t1.operate_name," +
            "t1.operater_code,t1.operate_time, case t4.security_id when security_id  then 1 else 0 end type from tb_employee_insurance t1 " +
            "left join tb_employee_info t2 on t1.oid = t2.oid " +
            "left join tb_insurance t3 on t1.insurance_id = t3.id " +
            "left join tb_employee_security t4 on t1.oid = t4.oid " +
            "<where> t1.is_delete = 0 " +
            "<if test='orgIds!=null and orgIds.size>0'> and t1.unit_id in " +
            " <foreach item=\"orgId\" index=\"index\" collection=\"orgIds\" open=\"(\" separator=\",\" close=\")\">#{orgId}</foreach>" +
            "</if>"+
            "<if test='orgIds==null or orgIds.size==0'> and t1.unit_id = null  </if>" +
            "<if test='orgId!=null and orgId.size>0'> and  t2.org_id in " +
            " <foreach item=\"o\" index=\"index\" collection=\"orgId\" open=\"(\" separator=\",\" close=\")\">#{o}</foreach>" +
            "</if>" +
            "<if test='orgId!=null and orgId.size ==0'> and t2.org_id = null </if>" +
            "<if test='companyName !=null and companyName!=&apos;&apos;'> and t3.company_name like '%${companyName}%' </if>"+
            "<if test='employeestatus !=null and employeestatus!=&apos;&apos;'> and t2.employeestatus = #{employeestatus}</if>"+
            "<if test='securityStatus !=null and securityStatus == 0'> and t4.security_id is null </if>"+
            "<if test='securityStatus !=null and securityStatus == 1'> and t4.security_id is not null </if>"+
            "<if test='status !=null '> and t1.status = #{status}</if>"+
            "<if test='operateName !=null and operateName!=&apos;&apos;'> and t1.operate_name like '%${operateName}%'</if>"+
            "</where>" +
            "order by t1.operate_time desc </script>")
    List<InsuranceMemberRecordDto> getMemberChangeList(@Param("orgIds") List<Long>orgIds,@Param("companyName")String companyName,@Param("employeestatus")String employeestatus,@Param("status") Integer status,
                                                       @Param("securityStatus")Integer securityStatus,@Param("operateName")String operateName, @Param("orgId")List<Long> orgId,IPage<InsuranceMemberRecordDto> page);

    @Select("select t1.*,t2.org_id,t2.job_id,t2.hiredate,t2.employeestatus from tb_employee_insurance t1 " +
            "left join tb_employee_info t2 on t1.oid = t2.oid  " +
            "where insurance_id = #{insuranceId} and t1.is_delete = 0 and t1.status = 1 ")
    List<EmployeeInsuranceDto> getMemberList(@Param("insuranceId") Long insuranceId);

}
